/*
 * 类名称:AdminLogActionServiceImpl.java
 * 包名称:com.unimall.graded.admin.api.logaction
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-03-17 09:33:11        lin     初版做成
 *
 * Copyright (c) 2019-2019 浵浵科技
 */
package com.unimall.graded.admin.api.logaction;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.unimall.graded.data.domain.LogActionDO;
import com.unimall.graded.data.mapper.LogActionMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.apache.ibatis.session.RowBounds;

import java.util.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Admin用户操作日志ServiceImpl实现类
 *
 * @author lin
 * @date 2020-03-17 09:33:11
 */
@Service
public class AdminLogActionServiceImpl implements AdminLogActionService {

    @Autowired
    private LogActionMapper logActionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer create(Long adminId, LogActionDO logActionDO) throws ServiceException{
        if (logActionDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
           if( StringUtils.isEmpty(logActionDO.getUrl()) ){
            throw new AdminServiceException("操作URL不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
       
        Date now = new Date();
        logActionDO.setId(null);
        logActionDO.setGmtCreate(now);
        logActionDO.setGmtUpdate(now);
        return logActionMapper.insert(logActionDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long adminId,Long id) throws ServiceException{
        return logActionMapper.delete(new EntityWrapper<LogActionDO>()
                .eq("id", id)) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(Long adminId,LogActionDO logActionDO) throws ServiceException{
        if (logActionDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
                 if( StringUtils.isEmpty(logActionDO.getUrl()) ){
                throw new AdminServiceException("操作URL不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
            
        Date now = new Date();
        logActionDO.setGmtUpdate(now);
        return logActionMapper.updateById(logActionDO) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogActionDO info(Long adminId,Long id) throws ServiceException{

        if(id == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }

        return logActionMapper.selectById(id);
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<LogActionDO> list(Long adminId,
Long id,
Long userId,
String url,
String postData,
 Integer page,
         Integer limit) throws ServiceException{

        Wrapper<LogActionDO> wrapper = new EntityWrapper<LogActionDO>();
        wrapper.orderBy("id", false);

            if( id == null ){
            wrapper.eq("id", id);
        }
            if( userId == null ){
            wrapper.eq("user_id", userId);
        }
            if( StringUtils.isEmpty(url) ){
            wrapper.eq("url", url);
        }
            if( StringUtils.isEmpty(postData) ){
            wrapper.eq("post_data", postData);
        }
                                                    
        List<LogActionDO> LogActionDOs = logActionMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = logActionMapper.selectCount(wrapper);
        return new Page<LogActionDO>(LogActionDOs, page, limit, count);
    }
}